/*
 * @Description:
 * @Author: Rfan
 * @Date: 2021-12-22 22:50:15
 * @LastEditTime: 2023-03-08 17:48:02
 */
import { message, Modal } from 'antd';
import axios from 'axios';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { appActions, appState } from '@/store/mobx';
import { getToken } from './auth';
// import { AxiosCanceler } from '@/utils/axiosCancel';
// import { basePort, baseUrl } from '@/routes/config';
// import store from '@/store';
// import { getToken, getUserId } from '@/utils/auth';

// 环境变量
const env = process.env.NODE_ENV;

// const axiosCanceler = new AxiosCanceler();

// create an axios instance
const requestInstance = (basePath) => {
  const service = axios.create({
    baseURL: `${env === 'development' ? '' : 'http://192.168.0.202:8888'}${basePath}`,
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 60 * 1000 // request timeout
  });

  // request interceptor
  service.interceptors.request.use(
    (config) => {
      // * 将当前请求添加到 pending 中
      // axiosCanceler.addPending(config);
      // do something before request is sent
      if (appState.token) {
        config.headers.Authorization = getToken();
      }
      return config;
    },
    (error) => {
      // do something with request error
      console.log(error); // for debug
      return Promise.reject(error);
    }
  );

  // response interceptor
  service.interceptors.response.use(
    (response) => {
      const { data: res, config } = response;
      // * 在请求结束后，移除本次请求(关闭loading)
      // axiosCanceler.removePending(config);
      // 返回流的直接成功
      if (['arraybuffer', 'blob'].indexOf(config.responseType) >= 0) {
        return res;
      }

      // if the custom code is not 200, it is judged as an error.
      if (res.code !== 200) {
        message.error({
          content: res.message || 'Error'
        });

        if (res.code === 401) {
          console.log('code not 200: ', res.code, res.message, res);
          Modal.destroyAll();
          // to re-login
          Modal.confirm({
            title: '登出',
            content: '您的登录已超时，请重新登录',
            okText: '重新登录',
            cancelText: '取消',
            icon: <ExclamationCircleOutlined />,
            onOk() {
              appActions.resetToken().then(() => {
                window.location.reload();
              });
            },
            onCancel() {
              console.log('Cancel');
            }
          });
        }
        return Promise.reject(res || 'Error');
      }
      return res;
    },
    (error) => {
      message.error({
        content: error.message
      });
      return Promise.reject(error);
    }
  );
  return service;
};

// baseURL: '/archive-biz/v1.0', // url = base url + request url
// baseURL: 'http://192.168.0.200/v1.0', // url = base url + request url
export default requestInstance('/archive-biz/v1');

// 配置接口
export const configRequest = requestInstance('/system-config/v1');

//
export const bizZipRequest = requestInstance('/archive-biz-zip/v1');

// jpams接口
export const jpamsRequest = requestInstance('/archive-biz');

// minio上传接口
export const minioRequest = requestInstance('/minio');

// 生成axios实例的工厂函数
export const request = requestInstance;

// 借阅工作流接口
export const workflowRequest = requestInstance('/archive-biz/v1/borrow-apply-form');

// 赋权工作流接口
export const rightsWorkflowRequest = requestInstance('/archive-biz/v1/process/process-rights');

// 小莫报表接口
export const reportRequest = requestInstance('/report');

// camunda流程相关
export const flowRequest = requestInstance('/camunda/v1');
// camunda流程相关
export const flowRequestV2 = requestInstance('/camunda/v2');
// camunda流程相关
export const logRequest = requestInstance('/process-log/v1');
// 统计相关相关
export const statRequest = requestInstance('/data-analysis/v1');
